package com.example.handler;

import com.example.entity.SysEmail;
import com.example.entity.SysTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Random;

@Service
public class SysEmailHandler extends AbstractSysTaskHandler {

    Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    public Boolean isMatch(SysTask sysTask) {
        return sysTask.getModelName().equalsIgnoreCase(SysEmail.class.getName());
    }

    @Override
    public void handler(SysTask sysTask) throws Exception {
        int size = new Random().nextInt(10);
        Thread.sleep(size * 1000);
        if (sysTask.getModelId() % 9 > 0) {
            throw new Exception("邮件发送失败...");
        }
        logger.info("SysEmailHandler 发送邮件: modelId: {} , modelName: {} ", sysTask.getModelId(), sysTask.getModelName());
    }

}
